﻿using BaseUtils;
using BaseUtils.Logger;
using NW_WEB_Service.com.app.entities.modal;
using Service;
using Service.src.com.utils.modal;
using Service.src.NW.Model;
using Service.src.NW.UI;
using System;
using System.Diagnostics;
using System.Linq;
using System.Web.Http;
using System.Windows.Forms;

namespace NW_WEB_Service.com.app.controllers
{
    [RoutePrefix("api/system")]
    public class SystemController : ApiController
    {

        [HttpGet]
        [Route("config")]
        public HttpResult GetSystemConfig()
        {
            return new HttpResult().Success(BaseService.AppConfig);
        }

        [HttpPost]
        [Route("open-file")]
        public HttpResult OpenFile([FromBody] OpenFileDTO OpenFile)
        {
            HttpResult Result = new HttpResult();
            try
            {
                if (string.IsNullOrEmpty(OpenFile.Path))
                {
                    OpenFile.Path = AppDomain.CurrentDomain.BaseDirectory;
                }
                Process.Start(new ProcessStartInfo
                {
                    FileName = OpenFile.Path + OpenFile.FileName,
                    UseShellExecute = true
                });
                Result.IsSuccess = true;
            }
            catch (Exception ex)
            {
                LoggerService.Error($"文件打开失败，地址{OpenFile.Path + OpenFile.FileName}，原因：{ex.Message}", ex);
                Result.IsSuccess = false;
            }
            return Result;
        }


        [HttpGet]
        [Route("open-form")]
        public HttpResult OpenForm([FromUri] string Uri)
        {
           // NWStudioHomeForm nWStudioHomeForm = new NWStudioHomeForm();
            //nWStudioHomeForm.Show();
            return new HttpResult().Success();
            //try
            //{
            //    // 构建完整URL（根据实际需求调整）
            //    string fullUrl = BaseService.AppConfig.field.SupplierName == "NWP1"
            //        ? $"file://{AppData.AppPath}dist/index.html#/{Uri}"
            //        : $"http://localhost:5173/#/{Uri}";

            //    // 调用系统默认浏览器打开URL
            //    Process.Start(new ProcessStartInfo(fullUrl)
            //    {
            //        UseShellExecute = true // 关键：启用系统外壳执行，自动关联浏览器
            //    });

            //    return new HttpResult(true);
            //}
            //catch (Exception ex)
            //{
            //    LoggerService.Error($"打开URL失败：{ex.Message}", ex);
            //    return new HttpResult(false);
            //}
            //try
            //{
            //    // 确保UI线程已初始化
            //    if (!UIThreadManager.IsInitialized)
            //    {
            //        UIThreadManager.Init();
            //    }

            //    // 通过UI线程管理器在STA线程中执行窗体操作
            //    UIThreadManager.Invoke(() =>
            //    {
            //        // 检查窗体是否已打开
            //        var existingForm = Application.OpenForms
            //            .OfType<NWUIGeneratorForm>()
            //            .FirstOrDefault(f => f.ViewHandler?.uri == Uri);

            //        if (existingForm != null)
            //        {
            //            // 若已打开，激活窗体
            //            existingForm.Activate();
            //            isOpen = true;
            //        }
            //        else
            //        {
            //            // 新建窗体并显示
            //            var newForm = new NWUIGeneratorForm(Uri);
            //            newForm.Show(); // Show()必须在STA线程调用
            //            isOpen = true;
            //        }
            //    });
            //}
            //catch (Exception ex)
            //{
            //    LoggerService.Error($"打开窗体失败，Uri: {Uri}，原因: {ex.Message}", ex);
            //    isOpen = false;
            //}

            //return new HttpResult(false);
        }
    }
}
